<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class My_layout
{

    var $obj;
    var $layout;

    function My_layout($layout = "")
    {
        $this->obj =& get_instance();
        $this->layout = $layout;
        $this->obj->load->model('mcountuser');
        $this->obj->load->model('mcountvisit');
    }

    function setLayout($layout)
    {
      $this->layout = $layout;
    }

    function view($view, $data=null, $return=false)
    {
        
        $loadedData = array();
        $loadedData['content_for_layout'] = $this->obj->load->view($view,$data,true);

        

        //---------------------------------------- load login -----------------------------------------

        $sess =$this->obj->session->userdata('users_Id');
        $data['isLogin'] = true;
        if($sess != null) {
            $data['isLogin'] =false;
            $data['users_Name'] = $this->obj->session->userdata('users_FullName');
        }   
        $loadedData['content_for_Login'] = $this->obj->load->view('frontend/after_login',$data,true);

        //---------------------------------------- truy cập thống kế -----------------------------------------

        $ip = $_SERVER["REMOTE_ADDR"]; 
        $agent = $_SERVER["HTTP_USER_AGENT"];
        $datetime = date("Y/m/d") . ' ' . date('H:i:s') ;
        $time = time();
        $time_check = $time-600; //SET TIME 10 Minute

        $checkExitsIP = $this->obj->mcountuser->checkExitsIP($ip);
        if(!$checkExitsIP) {
            $this->obj->mcountuser->insertIPAccess($ip, $agent, $datetime, $time);
        }
        
        $checkAdressAccess = $this->obj->mcountvisit->checkAddressVisit($ip);
        if(!$checkAdressAccess) {
            $this->obj->mcountvisit->insertIPAccess($ip, date("Y/m/d"), $time);
        }

        //------------------------------------------ Load all view ---------------------------------------

        $data['countOnline'] = $this->obj->mcountuser->countOnline();
        $data['countVisitALL'] = $this->obj->mcountvisit->countVisitALL();
        $data['countVisitMonth'] = $this->obj->mcountvisit->countVisitMonth();
        $data['countVisitToDay'] = $this->obj->mcountvisit->countVisitToDay();

        $this->obj->mcountuser->deleteIPAccess($time_check); //delete check online after 10 min

        $loadedData['content_for_count'] = $this->obj->load->view('frontend/statistics', $data, true); 
        if($return) {
            $output = $this->obj->load->view($this->layout, $loadedData, true);
            return $output;
        }
        else {
            $this->obj->load->view($this->layout, $loadedData, false);
        }
    }

    public function getProductInShoppingCart() {
        $data = $this->obj->cart->contents();
        foreach ($data as $key => $value) {
            foreach ($data[$key] as $item) {
                $data[$key]['gia_display'] = $this->format_price($data[$key]['price']);
                $data[$key]['gia_displayTotal'] = $this->format_price($data[$key]['price']*$data[$key]['qty']);
            }
        }
        return $data;
    }

    public function format_price($price) {
        $length = strlen($price);
        $newValue = '';
        while ($length > 0) {
            $temp =  substr($price, $length - 3, 3);
            if($length < 3) {
                $temp =  substr($price, 0, $length);
            }
            //echo($length -3);
            if(strlen($newValue)>0){
                $newValue = $temp.'.'.$newValue;
            } else {
                $newValue = $temp;
            }
            $length = $length -3;
        }
        return $newValue;
    }
    
}